package com.laozj.test.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.laozj.test.config.RabbitMQConfig;
import com.laozj.test.dao.UserDao;
import com.laozj.test.entity.UserEntity;
import com.laozj.test.service.UserService;
import com.laozj.test.vo.UserVO;
import io.swagger.util.Json;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {
    @Override
    public List<UserEntity> getUserList(UserVO queryVO) {

        long startTime = System.currentTimeMillis();

        List<UserEntity> userEntities = this.baseMapper.selectByParams(queryVO);

        log.info("查询耗时：{}",(System.currentTimeMillis() - startTime));
        return userEntities;
    }

    @Override
    public List<UserVO> update(List<UserVO> userVOList) {
        if(userVOList == null){
            return null;
        }
        List<UserEntity> collect = userVOList.stream().map(item -> {
            UserEntity userEntity = new UserEntity();
            BeanUtils.copyProperties(item, userEntity);
            return userEntity;
        }).collect(Collectors.toList());

        this.updateBatchById(collect);
        return userVOList;
    }

    @Override
    public void save(List<UserVO> userVOList) {
        if(userVOList == null){
            return;
        }
        List<UserEntity> collect = userVOList.stream().map(item -> {
            UserEntity userEntity = new UserEntity();
            BeanUtils.copyProperties(item, userEntity);
            return userEntity;
        }).collect(Collectors.toList());

        this.saveBatch(collect);
    }

    @Override
    public boolean leGuanLock(UserVO userVO) {

        UserEntity userEntity = this.baseMapper.selectById(userVO.getId());
        if(userEntity.getAge() <= 0){
            System.out.println("age小于0,不可操作");
            return false;
        }
        int version = userEntity.getVersion();

        userVO.setVersion(version);
        boolean sqlAns = this.baseMapper.leGuanSql(userVO);

        return sqlAns;
    }

    @Transactional
    @Override
    public boolean peiGuanLock(UserVO userVO) {
        UserVO vo = this.baseMapper.peiGuanSql(userVO);

        if(vo.getAge() < 0){
            System.out.println("年龄小于0,不可操作");
            return false;
        }
        boolean ans = this.baseMapper.updateAgeById(vo);

        return ans;
    }


    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Override
    public boolean RBLock(UserVO userVO) {

        // 在redis中操作数量
        Object str = redisTemplate.opsForValue().get(userVO.getId().toString());
        if(str == null){
            log.info("redis中无此数据");
            return false;
        }

        Long aLong = redisTemplate.opsForValue().decrement(userVO.getId().toString(), 2);
        if(aLong == null){
            log.info("找不到"+userVO.getId());
            return false;
        }
        if(aLong < 0){
            log.info("数量不足,不可操作");
            redisTemplate.opsForValue().increment(userVO.getId().toString(), 2);
            return false;
        } else if(aLong == 0){
            // 最后发送消息给rabbitmq
            log.info("aLong == 0 发送消息");
            userVO.setAge(0);
            userVO.setId(userVO.getId());
            rabbitTemplate.convertAndSend(RabbitMQConfig.USER_EXCHANGE, RabbitMQConfig.USER_ROUTING_KEY, JSON.toJSON(userVO));
        }
        return true;
    }

    @Override
    public void saveEntity(UserVO message) {
        UserVO userVO = new UserVO();
        userVO.setId(message.getId());
        List<UserEntity> userEntities = this.baseMapper.selectByParams(userVO);
        UserEntity entity = userEntities.get(0);
        entity.setAge(0);

        this.updateById(entity);
    }
}
